package com.laiyizhan.cms.first.service.impl;

import com.github.pagehelper.util.StringUtil;
import com.laiyizhan.cms.first.request.FirstRequest;
import com.laiyizhan.cms.first.service.IFirstService;
import com.laiyizhan.cms.manager.request.AdminUserRequest;
import com.laiyizhan.common.mapper.*;
import com.laiyizhan.common.model.*;
import com.sun.org.apache.bcel.internal.generic.SWITCH;
import javafx.scene.control.TextFormatter;
import org.apache.ibatis.javassist.expr.NewArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

import static org.springframework.beans.BeanUtils.copyProperties;
import static org.springframework.beans.BeanUtils.resolveSignature;

/**
 * Created by SoulWolf on 2017/7/15.
 */

@Service
public class FirstServiceImpl implements IFirstService {

    @Autowired
    First_messageMapper first_messageMapper;
    @Autowired
    First_aMapper first_aMapper;
    @Autowired
    First_bMapper first_bMapper;
    @Autowired
    First_cMapper first_cMapper;
    @Autowired
    First_dMapper first_dMapper;
    @Autowired
    First_eMapper first_eMapper;
    @Autowired
    StateMapper stateMapper;


    public State First_index(FirstRequest request) {
        State temp = stateMapper.selectByName(request.getoName());
        if (temp == null) {
          State state = new State();
          state.setHospital(request.getoName());
          state.setA("0");
          state.setB("0");
          state.setC("0");
          state.setD("0");
          state.setE("0");
          state.setF("0");
          state.setG("0");
          stateMapper.insertSelective(state);
          return state;
        }else {
          return stateMapper.selectByName(request.getoName());
      }
    }



    private String[] a_c = {"高级职称","中级职称","初级职称","其他","合计"};
    private String[] a_h = {"年平均在职职工人数（人）","卫生技术人员（人）","其他技术人员（人）","管理人员（人）","工勤技能人员 (人)","临时工（人）（选填）","在职职工年人均工资（万元/人）"};

    private String[] b_c = {"各级政府","国际组织","国际社会组织"};

    private String[] c_h = {"年度收费总额（元）","服务量（人次）","次均收费水平（元/人次）","卫生材料支出（元）","药品支出（元）"};


    public String First_a(FirstRequest request){
        /*查询该表是否已经填过 如果填过则删除原数据*/
        Check_message("a",request);

        /*存入个人信息*/
        First_message first_message = new First_message();
        copyProperties
                (request,first_message);
        first_messageMapper.insertSelective(first_message);

        /*数据处理*/
        float[] temp =  new float[35];
        for(int i=0;i<35;i++){
            if (request.getFirstA()[i]!=""){
                temp[i] = Float.parseFloat(request.getFirstA()[i]);
            }
        }
        /*计算前五行的第一列合计*/
        for(int l=0;l<28;l=l+7){
            temp[l]=temp[l+1]+temp[l+2]+temp[l+3]+temp[l+4];
            /*新添加的验证，当人数不为0的时候，工资不能为0*/
            if(temp[l]!=0 || temp[l+5]!=0 ){
              if( temp[l+6]==0){
                  return "no";
                }
            }
        }
        /*计算第六行的前五列合计*/
        for(int i=0;i<6;i++){
            temp[i+28]=temp[i+7]+temp[i+14]+temp[i+21]+temp[i];
        }


        /*生成对象，存入数据库*/
        int num=0;
        List<First_a> list = new ArrayList<First_a>();
        for(int i=0;i<5;i++) {
            for (int j = 0; j < 7; j++) {
                First_a first_a = new First_a();
                first_a.setName(request.getoName());
                first_a.setType(a_c[i]);
                first_a.setcType(a_h[j]);
                first_a.setCount(String.valueOf(temp[num]));
                list.add(first_a);
                num=num+1;
            }

        }

        first_aMapper.insertBatch(list);
        /*改变该机构的状态  使该表变为已填*/
        Change_state("a",request.getoName());
        return "ok";
    }

    public String First_b(FirstRequest request){
        Check_message("b",request);

        if(request.getFirstB()!=null) {
            request.getFirstB()[0] = "本机构财政项目支出补助收入";
        }
        /*数据处理*/
        float b_count=0F;
        for(int i=1;i<102;i=i+2){
            if(request.getFirstB()[i]!=""){
            b_count=b_count+Float.parseFloat(request.getFirstB()[i]);
            }
        }


        List<First_b> list = new ArrayList<First_b>();
        if(request.getFirstB()!=null){
            request.getFirstB()[1]=String.valueOf(b_count);
            for(int j=0;j<122;j=j+2){
                First_b first_b = new First_b();
                /*医院名称固定，由前一页传递过来*/
                first_b.setHosptial(request.getoName());
                if(j<102){
                    first_b.setSource(b_c[0]);
                }else if(j<112){
                    first_b.setSource(b_c[1]);
                }else {
                    first_b.setSource(b_c[2]);
                }
                first_b.setProjectName(request.getFirstB()[j]);
                first_b.setCount(request.getFirstB()[j+1]);
                list.add(first_b);
            }

        /*改变该机构的状态  使该表变为已填*/
            Change_state("b",request.getoName());
          first_bMapper.insertBatch(list);
        }
        return "ok";
    }

    public String First_c(FirstRequest request){
        Check_message("c",request);
        /*先把前台传过来的数据整合*/
        String [] all_first_c = new String[450];//字符串型的所有数据
        String[] all_out_c = new String [75];
        float[] c_f = new float[450];//浮点型的所有数据，汉字的部分默认0.0
        float[] new_out = new float[75];//新增的数据 支出情况
        for(int i = 0;i<450;i++){
            if(i<252){all_first_c[i]=request.getFirstC()[i];}
            else{all_first_c[i]=request.getFirstCo()[i-252];}
            if((i%6)!=0 && all_first_c[i]!=""){c_f[i]=Float.parseFloat(all_first_c[i]);}
        }
       for(int w=1;w<75;w++){
            all_out_c[w]=request.getFirstCl()[w];
           if(all_out_c[w]!=null && all_out_c[w]!="" ){new_out[w]=Float.parseFloat(request.getFirstCl()[w]);}
        }
        /*计算必须项的合计数据*/

        /*计算第一列的合计*/
        for(int m=0;m<450;m=m+6){
            if(all_first_c[m]!=""){
                c_f[m+1]=c_f[m+2]*c_f[m+3];
            }
        }
        /*计算行的合计  包含了新增的支出列*/
        int[] start = {1,25,79,127,193,235,343};
        int[] round = {3,8,7,8,6,16,6};
        int[] out = {0,4,13,21,32,39,57};
        for(int i=0;i<7;i++){
            for(int j=0;j<5;j++){
                for(int k=0;k<round[i];k++){
                    c_f[start[i]+j]=c_f[start[i]+j]+c_f[(start[i]+j)+6*(k+1)];
                }
            }
            for(int m=0;m<round[i];m++){
                new_out[out[i]]=new_out[out[i]]+new_out[out[i]+m+1];
            }
        }

        /*转为字符串类型赋值给all_first_c*/
        for(int n=0;n<450;n++){
            if((n%6)!=0 && n<420){
                all_first_c[n]=String.valueOf(c_f[n]);
            }
            if(n>419 && (n%6)==0 && all_first_c[n]!=""){
                for(int p=1;p<6;p++){
                    all_first_c[n+p]=String.valueOf(c_f[n+p]);
                }
            }
            if(n<70){all_out_c[n]=String.valueOf(new_out[n]);}
        }
        /*将服务项和次均收费水平清空*/
        for(int u=0;u<7;u++){
            all_first_c[start[u]+1]="";
        }
        for(int u=0;u<7;u++){
            all_first_c[start[u]+2]="";
        }

        /*将数据生成对象存储到数据库*/
        List<First_c> list = new ArrayList<First_c>();
        for(int i=0;i<450;i=i+6){
           for(int j=0;j<5;j++) {
               First_c first_c = new First_c();
               first_c.setType(all_first_c[i]);
               first_c.setHospital(request.getoName());
               first_c.setDepartment(request.getFirstCdepartment());
               first_c.setcType(c_h[j]);
               first_c.setCount(all_first_c[i+j+1]);
               list.add(first_c);
           }
           /*增加 支出列至list*/
            First_c first_c = new First_c();
            first_c.setType(all_first_c[i]);
            first_c.setHospital(request.getoName());
            first_c.setDepartment(request.getFirstCdepartment());
            first_c.setcType("其他支出（元）");
            first_c.setCount(String.valueOf(all_out_c[i/6]));
            list.add(first_c);
        }

        /*改变该机构的状态  使该表变为已填*/
        Change_state("c",request.getoName());
        first_cMapper.insertBatch(list);
        return "ok";
    }

    public String First_d(FirstRequest request) {
        Check_message("d",request);
        List<First_d> list = new ArrayList<First_d>();
        if(request.getFirstD()!=null){
            for(int i=0;i<40;i=i+2){
                First_d first_d = new First_d();
                first_d.setHospital(request.getoName());
                first_d.setName(request.getFirstD()[i]);
                if (i < 30) {
                    if(request.getFirstD()[i+1]!=""){first_d.setCount(request.getFirstD()[i+1]);}
                    else{first_d.setCount("0");}
                }else{
                    first_d.setCount(request.getFirstD()[i+1]);
                }

                list.add(first_d);
            }
        }

        /*改变该机构的状态  使该表变为已填*/
        Change_state("d",request.getoName());
        first_dMapper.insertBatch(list);
    return "ok";
    }

    public String First_e(FirstRequest request) {
        Check_message("e",request);
        List<First_e> list = new ArrayList<First_e>();

        for (int i = 0; i < 104; i = i + 4) {
            First_e first_e = new First_e();
            first_e.setHospital(request.getoName());
            first_e.setName(request.getFirstE()[i]);
            first_e.setType(request.getFirstE()[i + 1]);
            if (i < 44) {
                if(request.getFirstE()[i+2]!=""){
                    first_e.setCount(request.getFirstE()[i+2]);
                } else{
                    first_e.setCount("0.0");
                }
                if(request.getFirstE()[i+3]!="") {
                    first_e.setPrice(request.getFirstE()[i + 3]);
                }else{
                    first_e.setPrice("0.0");
                }
            }else{
                first_e.setCount(request.getFirstE()[i+2]);
                first_e.setPrice(request.getFirstE()[i + 3]);
            }
            list.add(first_e);
        }

        /*改变该机构的状态  使该表变为已填*/
        Change_state("e",request.getoName());
        first_eMapper.insertBatch(list);
        return "ok";
    }

    public String Check_message(String table,FirstRequest request){
        switch (table)
        {
            case "a":
                if(first_aMapper.selectByName(request.getoName())!=null){
                    first_aMapper.deleteByName(request.getoName());
                }
                break;
            case "b":
                if(first_bMapper.selectByName(request.getoName())!=null){
                    first_bMapper.deleteByName(request.getoName());
                }
                break;
            case "c":
                First_c first_c = new First_c();
                first_c.setHospital(request.getoName());
                first_c.setDepartment(request.getFirstCdepartment());
                if(first_cMapper.selectByName(first_c)!=null){
                    first_cMapper.deleteByName(first_c);
                }
                break;
            case "d":
                if(first_dMapper.selectByName(request.getoName())!=null){
                    first_dMapper.deleteByName(request.getoName());
                }
                break;
            case "e":
                if(first_eMapper.selectByName(request.getoName())!=null){
                    first_eMapper.deleteByName(request.getoName());
                }
                break;
        }

        return "ok";
    }

    private String Change_state(String table,String oName) {
        State state = stateMapper.selectByName(oName);
        switch (table)
        {
            case "a":
                state.setA("1");
                break;
            case "b":
                state.setB("1");
                break;
            case "c":
                state.setC("1");
                break;
            case "d":
                state.setD("1");
                break;
            case "e":
                state.setE("1");
                break;
        }
        stateMapper.updateByName(state);
        return "ok";
    }






}
